<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <!--{include file='comm/header.tpl'}-->
    <title>商品销量控制</title>
    <!--{include file="comm/icheck.tpl"}-->
</head>
<body>

<div class="ebsig_container">
    <div class="comm_content">
        <form id="sale_form" class="form-horizontal" role="form" onsubmit="return false;">

            <div class="form-group">
                <label for="cityID" class="col-sm-2 control-label"><span class="red">*</span> 城市：</label>
                <div class="col-sm-9">
                    <select name="cityID" id="cityID" class="form-control w150" >
                        <!--{foreach from=$city item=c}-->
                        <!--{if $c.id == 110}-->
                            <option value="<!--{$c.id}-->" selected><!--{$c.name}--></option>
                        <!--{else}-->
                            <option value="<!--{$c.id}-->"><!--{$c.name}--></option>
                        <!--{/if}-->

                        <!--{/foreach}-->
                    </select>
                </div>
            </div>


            <div class="form-group">
                <label for="goodsName" class="col-sm-2 control-label"><span class="red">*</span> 商品名称：</label>
                <div class="col-sm-9">
                    <input type="text" class="form-control w300" id="goodsName" name="goodsName" value="" onblur="saleControl.checkGoods()" placeholder="请输入商品名称">
                </div>
            </div>

            <div class="form-group" style="display: none;" id="saletotal_form">
                <label for="saletotal" class="col-sm-2 control-label"> 商品销量：</label>
                <div class="col-sm-9">
                    <input type="text" readonly class="form-control w150" id="saletotal" name="saletotal" value="" onblur="saleControl.checkGoods()" placeholder="请输入商品名称">
                </div>
            </div>

            <input type="text" name="postID" value="" id="postID" style="display: none;">


            <div class="form-group">
                <label for="changeSaleType" class="col-sm-2 control-label"><span class="red">*</span> 可变类型：</label>
                <div class="col-sm-9">
                    <div class="fl" style="margin-right: 10px;">
                        <input type="radio" class="square-radio" id="square-radio-1" name="changeSaleType"  value="0" checked>
                        <label for="square-radio-1">增加</label>
                    </div>
                    <div class="fl">
                        <input type="radio" class="square-radio" id="square-radio-0" name="changeSaleType"  value="1">
                        <label for="square-radio-0">减少</label>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="changeSaleTotal" class="col-sm-2 control-label"><span class="red">*</span> 可变数量：</label>
                <div class="col-sm-9">
                    <input type="text" class="form-control w150" id="changeSaleTotal" name="changeSaleTotal" value="" onblur="saleControl.checkGoods()" placeholder="请输入可变数量">
                </div>
            </div>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button type="button" class="btn btn-primary" onclick="saleControl.saveInfo();">保存</button>
                </div>
            </div>

        </form>


    </div>

</div>

<script type="text/javascript">
    var saleControl= {

        //检查商品是否存在 存在返回商品销量
        checkGoods:function(){
            var cityID = $('#cityID').val();
            var goodsName = $('#goodsName').val();
            if( goodsName == '' ){
                E.alert('商品名称必须填写');
                return;
            }
            E.ajax_post({
                url: '/invokingAjax-backend/goods/goodsSaleControl.ajax?operFlg=1',
                data: {
                    cityID:cityID,
                    goodsName:goodsName
                },
                call: function( o ){
                    if( o.code == 200 ){
                        $('#postID').val( o.data.postID );
                        $('#saletotal').val(o.data.saletotal);
                        $('#saletotal_form').show();
                    }else{
                        $('#saletotal_form').hide();
                        E.alert( o.message );
                        $('#goodsName').val('');
                        return;
                    }
                }
            })
        },

        //保存销量控制信息
        saveInfo:function(){
            var dt = E.getFormValues('sale_form');

            var error = '';
            if( dt.cityID == '' ){
                error += '城市不能为空<br>';
            }

            if( dt.goodsName == ''){
                error += '商品名称不能为空<br>';
            }

            if( dt.changeSaleTotal == ''){
                error += '可变数量不能为空';
            }else{
                if( !E.isInt(dt.changeSaleTotal) || (dt.changeSaleTotal == 0) ){
                    error += '可变数量必须为正整数';
                }
                if( dt.changeSaleType == 1){
                    var saletotal = parseInt( dt.saletotal );
                    var changeSaleTotal = parseInt( dt.changeSaleTotal );
                    if( saletotal < changeSaleTotal ){
                        error += '可变类型为减少时，可变数量不能大于商品销量';
                    }
                }


            }

            if( error != ""){
                E.alert(error);
                return;
            }

            E.ajax_post({
                url: '/invokingAjax-backend/goods/goodsSaleControl.ajax?operFlg=2',
                data: dt,
                call: function( o ){
                    if( o.code == 200 ){
                        E.alert( o.message,2 ,saleControl.load );
                        return;
                    }else{
                        E.alert( o.message );
                        return;
                    }
                }
            });
        },

        load: function() {
            $('#goodsName').val('');
            $('#postID').val('');
            $('#saletotal').val('');
            $('#square-radio-1').iCheck('check');
            $('#saletotal_form').hide();
            $('#changeSaleTotal').val('');

        }
    };
    $(window).ready(function() {

        //单选框样式
        $('.square-radio').iCheck({
            checkboxClass: 'icheckbox_square-blue',
            radioClass: 'iradio_square-blue',
            increaseArea: '20%' // optional
        });


    });
</script>

</body>
</html>